package cn.jy.generate.utils;

import java.util.List;

/**
 * 转换
 *
 * @author ljw
 */
public class ConvertUtil {

	final static String[] SUFFIX = new String[] {"_java", "_xml", "vue_", "js_", "api_"};

	/**
	 * 数据库字段类型转java类型.
	 *
	 * @param dbColumnType 数据库类型
	 * @return java类型
	 */
	public static String dbColumnType2JavaProperties(String dbColumnType) {
		if (dbColumnType == null) {
			return null;
		}
		dbColumnType = dbColumnType.toLowerCase();
		if (dbColumnType.contains("char") || dbColumnType.contains("text")) {
			return "String";
		} else if (dbColumnType.contains("bigint")) {
			return "Long";
		} else if (dbColumnType.contains("int")) {
			return "Integer";
		} else if (dbColumnType.contains("bit")) {
			return "Integer";
		} else if (dbColumnType.equals("time")) {
			return "LocalTime";
		} else if (dbColumnType.equals("date")) {
			return "LocalDate";
		} else if (dbColumnType.contains("date") || dbColumnType.contains("time") || dbColumnType.contains("year")) {
			return "LocalDateTime";
//		} else if (dbColumnType.contains("bit")) {
//			return "Boolean";
		} else if (dbColumnType.contains("decimal")) {
			return "BigDecimal";
		} else if (dbColumnType.contains("blob")) {
			return "byte[]";
		} else if (dbColumnType.contains("float")) {
			return "BigDecimal";
		} else if (dbColumnType.contains("double")) {
			return "BigDecimal";
		} else if (dbColumnType.contains("json") || dbColumnType.contains("enum")) {
			return "String";
		}
		return "String";
	}


	/**
	 * 数据库字段转Java属性名.
	 *
	 * @param dbColumnName 数据库字段名
	 * @return Java属性名
	 */
	public static String dbColumnName2JavaProperties(String dbColumnName) {
		return underline2Camel(dbColumnName);
	}

	/**
	 * 数据库表名转类名.
	 *
	 * @param dbTableName 数据库表名
	 * @param removePrefix 删除前缀
	 * @return Java类名
	 */
	public static String dbTableName2ClassName(String dbTableName, List<String> removePrefix) {
		if (dbTableName == null) {
			return null;
		}
		for (String prefix : removePrefix) {
			if (dbTableName.startsWith(prefix)) {
				dbTableName = dbTableName.substring(prefix.length());
				break;
			}
		}
		String className = underline2Camel(dbTableName);
		if (className == null) {
			return null;
		}
		return className.substring(0, 1).toUpperCase() + className.substring(1);
	}

	/**
	 * 去除前缀和后缀
	 */
	public static String removePrefixAndSuffix(String str) {
		// 需要切除后缀或者前缀
		for (String suffix : SUFFIX) {
			if (str.contains(suffix)) {
				str = str.substring(0, str.indexOf(suffix));
			}
		}
		return str;
	}

	/**
	 * 大写第一个字母.
	 *
	 * @param str
	 */
	public static String upperFirst(String str) {
		if (str == null) {
			return null;
		}
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * 小写第一个字母.
	 *
	 * @param str
	 */
	public static String lowerFirst(String str) {
		if (str == null) {
			return null;
		}
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}


	/**
	 * 下划线转驼峰.
	 *
	 * @param source
	 * @return
	 */
	public static String underline2Camel(String source) {
		if (source == null) {
			return null;
		}
		source = source.toLowerCase();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < source.length(); i++) {
			char c = source.charAt(i);
			if (i > 0 && c == '_' && i < source.length() - 1) {
				sb.append(Character.toUpperCase(source.charAt(i + 1)));
				i++; // skip the underscore
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 大写字符串.
	 *
	 * @param source
	 * @return
	 */
	public static String toUpperString(String source) {
		if (source == null) {
			return null;
		}
		return source.toUpperCase();

	}

	/**
	 * 表别名.
	 *
	 * @param source
	 * @return
	 */
	public static String tableAlias(String source) {
		if (source == null) {
			return null;
		}
		String[] split = source.split("_");
		String alias = split[split.length - 1];
		return alias;

	}

	/**
	 * 根据表名生成一个由首字母组成的别名
	 *
	 * @param tableName 表名字符串，例如："ms_knowledge_article_category_rel"
	 * @return 生成的别名，例如："mkacr"
	 */
	public static String generateTableCodeAlias(String tableName) {
		if (tableName == null || tableName.isEmpty()) {
			return "";
		}

		String[] parts = tableName.split("_");
		StringBuilder alias = new StringBuilder();

		for (String part : parts) {
			if (!part.isEmpty()) {
				alias.append(part.charAt(0));
			}
		}

		return alias.toString().toLowerCase();
	}

}
